const http = require('http')
const fs = require('fs')
const path = require('path')
const qs = require('querystring')
const app = http.createServer()

app.on('request', (req, res) => {
  console.log(req.url);
  if (req.url == '/api/province') {
    fs.readFile(path.join(__dirname, './province.json'), (err, data) => {
      console.log(data.toString());
      res.end(data.toString())
    })
  } else if (req.url.split('?')[0] == '/api/city') {
    //获取城市的值
    fs.readFile(path.join(__dirname, './city.json'), (err, data) => {
      // console.log(qs.parse(req.url.split('?')[1])['pname']);
      var pname = qs.parse(req.url.split('?')[1])['pname']
      console.log(JSON.parse(data.toString())[pname]);
      res.end(JSON.stringify(JSON.parse(data.toString())[pname]))
    })
  } else {
    res.end('111')
  }
})

app.listen(7777, () => {
  console.log('http://127.0.0.1:7777');
})